// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
// 引用element ui自定义主题
import '../theme/index.css'
import store from './store'
import router from './router'
import common from './utils/common'
import filters from './utils/filters'
import axios from './utils/request'
import App from './App'
import './style/reset.less'
import './style/style.less'
import './style/theme.less'
import 'babel-polyfill'
import Echarts from 'echarts'

/*
* 引用echarts
* 单独引入bar line scatter pie四种基础图表
*/
// let echarts = require('echarts/lib/echarts') // 引入ECharts主模块
// // require('echarts/lib/chart/bar') // 引入柱状图
// require('echarts/lib/chart/line') // 引入折线图
// // require('echarts/lib/chart/scatter') // 引入散点图
// require('echarts/lib/chart/pie') // 引入饼状图
// require('echarts/lib/component/tooltip') // 引入提示框
// require('echarts/lib/component/title') // 引入标题组件
// require('echarts/lib/component/legend') // 引入标题组件

Vue.config.productionTip = false
Vue.use(ElementUI)

Vue.prototype.$http = axios
Vue.prototype.$method = common
Vue.prototype.$filter = filters
Vue.prototype.$echarts = Echarts

// 全局引入过滤器
Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
})

router.beforeEach((to, from, next) => {
    // next()
    if (to.path === '/login') {
        next()
    } else {
        setTimeout(() => {
            if (!store.getters.user) {
                store.dispatch('loginBySession').then(res => {
                    if (!res) {
                        next('/login')
                    } else {
                        common.setWebSocket()
                        routeRedirect(next, store, to, true)
                    }
                })
            } else {
                routeRedirect(next, store, to)
            }
        }, 0)
    }

    // 重定向路由
    function routeRedirect(next, store, to, replace) {
        let roleId = store.getters.user.roleId
        // 1 超级管理员 2 管理员 3 护士 4 保安
        if (roleId === 1) {
            goRoute(to, replace, () => {
                next('/monitor/home')
            })
        } else if (roleId === 2) {
            goRedirect(to, roleId, '/monitor/account-manage', replace)
        } else if (roleId === 3) {
            goRedirect(to, roleId, '/monitor/home', replace)
        } else if (roleId === 4) {
            goRedirect(to, roleId, '/monitor/temporary-card', replace)
        }
    }

    // 重定向方法抽离
    function goRedirect(to, roleId, path, replace) {
        if (to.meta.auth && to.meta.auth !== roleId) {
            goRoute(null, null, () => {
                next(path)
            })
        } else {
            goRoute(to, replace, () => {
                next(path)
            })
        }
    }

    // 跳转路由
    function goRoute(to, replace, callback) {
        if (to) {
            if (to.path !== '/') {
                if (replace) {
                    next({...to, replace})
                } else {
                    next(...to)
                }
            } else {
                if (callback) {
                    callback()
                } else {
                    next({...to, replace})
                }
            }
        } else {
            if (callback) callback()
        }
    }
})

// // 路由跳转后更新主题显示区域，刷新滚动条
router.afterEach((to, from) => {
    window.$history++
})

/* eslint-disable no-new */
window.$vm = new Vue({
    el: '#app',
    router,
    store,
    components: {App},
    template: '<App/>'
})

if (!window.localStorage.getItem('ajaxApi')) window.localStorage.setItem('ajaxApi', '/_api')
window.$history = 0
